package com.ibeeking.found.msg.rest.service.impl;

import com.ibeeking.found.common.constants.MqTopic;
import com.ibeeking.found.msg.rest.service.IMqService;
import com.ibeeking.found.msg.rest.service.mq.MqProducer;
import com.ibeeking.found.msg.service.common.param.MsgNoticeParam;
import com.ibeeking.nematos.log.utils.LogUtils;
import com.ibeeking.nematos.rocketmq.base.MessageBuilder;
import com.ibeeking.nematos.rocketmq.trace.common.MQHeaderNounConstant;
import com.ibeeking.nematos.utils.data.ParamCheckUtils;
import com.ibeeking.nematos.utils.data.StringUtils;
import org.apache.logging.log4j.ThreadContext;
import org.apache.rocketmq.common.message.Message;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
public class MqServiceImpl implements IMqService {

    @Resource
    private MqProducer mqProducer;

    @Async
    @Override
    public void sendMsgNoticeMessage(MsgNoticeParam param) {
        try {
            ParamCheckUtils.check(param);
            MessageBuilder messageBuilder = MessageBuilder.of(param).topic(MqTopic.MSG_NOTICE_TOPIC);
            if(StringUtils.isNotEmpty(param.getTenantId())) {
                ThreadContext.put(MQHeaderNounConstant.TENANT_ID, param.getTenantId());
            }
            Message message = messageBuilder.build();
            mqProducer.syncSend(message);
        } catch (Exception e) {
            LogUtils.error("发送同步任务Mq消息异常:{}", e);
        }
    }

}
